package com.avic.rong.goodsSort.fragment.gj;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.wedgit.customView.RoundImageView;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.adapter.GoodsItemRvAdapter;
import com.avic.rong.goodsSort.api.GoodsSortItemApi;
import com.avic.rong.goodsSort.bean.GoodsSortItemBean;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsGongJuFragment extends BaseFragment {

    @BindView(R.id.goods_GongJu_Rv)
    RecyclerView goodsGongJuRv;
    @BindView(R.id.goods_Gj_Img)
    RoundImageView goodsGjImg;
    private View view;
    private List<GoodsSortItemBean.InternalListBean> beanList = new ArrayList<>();
    private GoodsItemRvAdapter adapter;

    public GoodsGongJuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_goods_gongju, container, false);
        ButterKnife.bind(this, view);
        getData();
        return view;
    }

    private void getData() {
        GoodsSortItemApi goodsSortItemApi = new GoodsSortItemApi((GoodsSortActivity) getContext(), delCartListener, 4, "gjb");
        goodsSortItemApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortItemApi);
    }

    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            GoodsSortItemBean bean = (GoodsSortItemBean) subjects.getData();
            Log.e("cjx", "bz拿到数据");
            beanList.addAll(bean.getInternalList());
            setAdapter();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    private void setAdapter() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        adapter = new GoodsItemRvAdapter(getContext(), beanList,4);
        goodsGongJuRv.setLayoutManager(manager);
        goodsGongJuRv.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }
}
